package day8

import org.apache.spark.sql.{SaveMode, SparkSession}

import java.util.Properties

object Test2_jdbc {
  def main(args: Array[String]): Unit = {
    // 往mysql数据库中写入数据
    val spark: SparkSession = SparkSession.builder().master("local[*]").appName("yy").getOrCreate()

    import spark.implicits._

    val prop = new Properties()
    prop.setProperty("user","postgres")
    prop.setProperty("password","123456")
//    properties.setProperty("driver","com.mysql.jdbc.Driver")
    prop.setProperty("driver","org.postgresql.Driver")
    val df = List(("zs", 18, "男"), ("lisi", 20, "男"), ("紫霞", 22, "女"))
                                    .toDF("name","age","sex")

    //
    df.write.mode(SaveMode.Overwrite)
      .jdbc("jdbc:postgresql://hadoop10:5432/postgres","spark_user",prop)

    spark.stop()
  }
}
